/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.datetime;

import org.openaecfm.model.datetime.Duration;
import org.openaecfm.model.datetime.LagTime;
import org.openaecfm.model.datetime.SchedulingTime;
import org.openaecfm.model.datetime.TaskDurationEnum;
import org.openaecfm.model.measure.RatioMeasure;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;
import org.openaecfm.spf.model.TypedParameter;

public final class LagTimeIO extends AbstractEntityInstanceInputImpl<LagTime>
		implements IEntityInstanceOutput<LagTime> {

	private IEntityInstanceUtilOutput<SchedulingTime> utilOutput;
	private IEntityInstanceUtilInput<SchedulingTime> utilInput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<SchedulingTime> utilOutput) {
		this.utilOutput = utilOutput;
	}

	public void setUtilInput(IEntityInstanceUtilInput<SchedulingTime> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public String exportSPF(LagTime pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCLAGTIME",
				appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set LagValue
		if (pojo.getLagValue() instanceof RatioMeasure) {
			RatioMeasure r = (RatioMeasure) pojo.getLagValue();
			TypedParameter t = new TypedParameter("IFCRATIOMEASURE",
					new RealParameter(r.doubleValue()));
			ei.addParameter(t);
		} else if (pojo.getLagValue() instanceof Duration) {
			Duration d = (Duration) pojo.getLagValue();
			TypedParameter t = new TypedParameter("IFCDURATION",
					new StringParameter(d.toString(), StringParameter.UTF_8));
			ei.addParameter(t);
		}

		// Set DurationType
		ei.addParameter(new EnumerationParameter(pojo.getDurationType()));

		return ei.toString();
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei, LagTime pojo,
			IAppDataManager appDataManager) {
		return;
	}

	@Override
	protected LagTime doCreateObject() {
		return new LagTime();
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei, LagTime pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set LagValue
		TypedParameter lagValue = (TypedParameter) sei.getParameter(3);
		if (lagValue.getKeyword().equalsIgnoreCase("IFCRATIOMEASURE")) {
			RealParameter r = (RealParameter) lagValue.getParameter();
			pojo.setLagValue(new RatioMeasure(r.toDouble()));
		} else if (lagValue.getKeyword().equalsIgnoreCase("IFCDURATION")) {
			StringParameter s = (StringParameter) lagValue.getParameter();
			pojo.setLagValue(new Duration(s.toUTF8()));
		}

		// Set DurationType
		EnumerationParameter durationType = (EnumerationParameter) sei
				.getParameter(4);
		pojo.setDurationType(TaskDurationEnum.valueOf(durationType
				.toEnumString()));
	}
}
